package housekeeping.service.Impl;

import housekeeping.mapper.WorkerApplicationMapper;
import housekeeping.pojo.WorkerApplication;
import housekeeping.service.WorkerApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @Author Du
 * @Date 2025/7/7 17:01
 * @注释
 */
@Service
public class WorkerApplicationServiceImpl implements WorkerApplicationService {
    @Autowired
    private WorkerApplicationMapper workerApplicationMapper;

    @Override
    public List<WorkerApplication> getWorkerApplications() {
        List<WorkerApplication> list = workerApplicationMapper.selectList(null);
        return list;
    }

    @Override
    public WorkerApplication selectById(Integer id){
        WorkerApplication workerApplication = workerApplicationMapper.selectById(id);
        if(workerApplication != null){
            return workerApplication;
        }
        else{
            throw new RuntimeException("查询审批信息失败");
        }
    }

    @Override
    public void updateWorkerApplication(WorkerApplication workerApplication){
        WorkerApplication oldWorkerApplication = workerApplicationMapper.selectById(workerApplication.getId());
        if(oldWorkerApplication != null){
            int result = workerApplicationMapper.updateById(workerApplication);
            if(result < 1){
                throw new RuntimeException("更新失败");
            }
        } else{
            workerApplicationMapper.insert(workerApplication);
        }
    }

    @Override
    public void insertWorkerApplication(WorkerApplication workerApplication){
        workerApplicationMapper.insertWorkerApplication(workerApplication);
    }
}
